package com.heima.search.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.search.dtos.UserSearchDto;
import com.heima.model.search.pojos.ApUserSearch;
import com.heima.model.user.pojos.ApUser;
import com.heima.search.feign.BehaviorFeign;
import com.heima.search.mapper.ApUserSearchMapper;
import com.heima.search.service.ApUserSearchService;
import com.heima.utils.common.ApUserThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApUserSearchServiceImpl extends ServiceImpl<ApUserSearchMapper, ApUserSearch> implements ApUserSearchService {

    @Autowired
    private BehaviorFeign behaviorFeign;

    /**
     * 查询搜索历史
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findUserSearch(UserSearchDto dto) {

        //校验数据
        if (null == dto) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        //获取用户id
        ApUser apUser = ApUserThreadLocal.getUser();
        if (null == apUser) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        //获取行为实体
        ApBehaviorEntry byUserIdOrEntry = behaviorFeign.findByUserIdOrEntryId(apUser.getId(), dto.getEquipmentId());

        //获取搜索历史
        Page pageParam = new Page(0, dto.getPageSize());
        IPage page = page(pageParam, Wrappers.<ApUserSearch>lambdaQuery()
                .eq(ApUserSearch::getEntryId, byUserIdOrEntry.getId()).eq(ApUserSearch::getStatus, 1)
                .orderByDesc(ApUserSearch::getCreatedTime));

        return ResponseResult.okResult(page.getRecords());
    }

    /**
     * 删除搜索历史
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult delUserSearch(UserSearchDto dto) {

        //校验数据
        if (null == dto) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        //获取用户id
        ApUser apUser = ApUserThreadLocal.getUser();
        if (null == apUser) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        //获取行为实体
        ApBehaviorEntry byUserIdOrEntry = behaviorFeign.findByUserIdOrEntryId(apUser.getId(), dto.getEquipmentId());

        //修改状态
        update(Wrappers.<ApUserSearch>lambdaUpdate()
                .eq(ApUserSearch::getEntryId, byUserIdOrEntry.getId())
                .eq(ApUserSearch::getId, dto.getId())
                .set(ApUserSearch::getStatus, 0));

        return ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 插入搜索记录
     *
     * @param
     * @return
     */
    @Async("taskExecutor")
    @Override
    public void insert(Integer entryId, String searchWords) {

        //查询记录数据
        ApUserSearch userSearch = getOne(Wrappers.<ApUserSearch>lambdaUpdate()
                .eq(ApUserSearch::getEntryId, entryId)
                .eq(ApUserSearch::getKeyword, searchWords));

        //判断数据是否存在
        if (null != userSearch) {
            //存在修改状态
            if (userSearch.getStatus() == 0) {
                userSearch.setStatus(1);
                updateById(userSearch);
            }
        } else {
            //不存在保存数据
            userSearch = new ApUserSearch();
            userSearch.setStatus(1);
            userSearch.setEntryId(entryId);
            userSearch.setKeyword(searchWords);
            userSearch.setCreatedTime(new Date());

            //保存数据
            save(userSearch);
        }
    }
}
